Latest update: September 2017
This tutorial introduces the script execution function of FlashAir IoT Hub.
With the script execution function, you can execute arbitrary Lua script on FlashAir.
First of all, I will explain what kind of mechanism the script execution is realized.
Script execution is realized by polling on FlashAir and acquiring the job of FlashAir IoT Hub as follows.
Although it was possible to run Lua script via the Internet without using FlashAir IoT Hub, we needed the following preparations and knowledge.
FlashAir IoT Hub solves these problems by restricting access by safe and easy by using a token for communication with FlashAir and by setting FlashAir IoT Hub as an access mediation server.
Also, in script execution, you can use it with confidence without using FlashAir on the Internet by using the job mechanism.
Let's learn how to use script execution next.
Specify the path and argument (optional) of the Lua script you want to execute and click the Execute button.
Please note that the Lua script to be executed must be placed on FlashAir in advance.
Specify the file path of the Lua script on FlashAir.
Arbitrary arguments can be specified in the Lua script to be executed. Arguments must be specified in JSON format. You can specify only one argument, but you can specify multiple values by using JSON's array format or the like.
The path and argument of the entered Lua script are registered as a job and displayed in the list.
Here you can check the job execution status and execution result.
Finally, let's actually use script execution.
As an example we will create a Lua script that registers the value of the argument entered in FlashAir IoT Hub
as a
measured value.
Save the following Lua script in the root directory of FlashAir.
local iothub = require("iothub")
if type(arguments) == "table" then
iothub.addMeasurement(arguments)
sleep(30000)
iothub.addMeasurement(arguments)
end
arguments
by converting JSON format values into Lua variables.
arguments
is of the same table type as the argument of
iothub.addMesurement()
, we will process it.
arguments
to FlashAir IoT Hub as measured value in
iothub.addMesurement()
.
Also edit the sample script
bootscript.lua
that can be downloaded from FlashAir IoT Hub as follows and save it in the root
directory.
local iothub = require("iothub")
-- iothub.startPioUpload(0x03)
-- iothub.stopPioUpload()
local cnt = 0
while(1) do
iothub.runJob()
--iothub.addMeasurement({10, 20, cnt})
--cnt = cnt + 1
sleep(10000)
collectgarbage("collect")
end
iothub.runJob()
to execute the job if you can get the job from FlashAir IoT Hub.
echo.lua
is easy to understand.
You are now ready to run the script.
Let's execute script from FlashAir IoT Hub.
Enter
/echo.lua
for the path and
[40, 50]
for the argument and click the execute button.
The script was executed and succeeded. Let's take a look at the measured value graph.
You should be able to see that the measured values sent in
echo.lua
are reflected in the graph.
Preliminary preparation of Lua script enables detailed control such as acquisition and setting of GPIO and sensor data, so please make use of the script execution function of FlashAir IoT Hub by all means.